package com.woniu.bank.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.bank.dto.ClientInfoDTO;
import com.woniu.bank.dto.NewClientfoDTO;
import com.woniu.bank.entity.ClientInfoSheet;
import com.woniu.bank.service.ClientInfoService;
import com.woniu.bank.service.MemberService;
import com.woniu.bank.util.Result;
import com.woniu.bank.vo.ClientInfoSheetRatedVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: chench
 * @time: 2023/1/4 15:14
 */
@RestController
@RequestMapping("client")
@Api(tags = "客户建档接口", description = "所有客户建档基础数据接口")
public class ClientInfoController {
    @Autowired
    private ClientInfoService clientInfoService;
    @Autowired
    private MemberService memberService;

    @PostMapping("selectAll")
    @ApiOperation(value = "客户建档查询全部", notes = "客户建档查询全部、模糊查询和分页", httpMethod = "POST")
    public Result selectAll(@RequestBody ClientInfoDTO clientInfoDTO) {
        /**
         * @description:客户建档查询全部、模糊查询和分页
         * @param clientInfoDTO
         * @return: com.woniu.bank.util.Result
         * @author: chench
         * @time: 2023/1/4 16:33
         */
        List<ClientInfoSheet> clientInfoList = clientInfoService.selectAll(clientInfoDTO);
        PageInfo<ClientInfoSheet> clientInfoSheetPageInfo = new PageInfo<>(clientInfoList);
        if (clientInfoSheetPageInfo==null) {
            return Result.error("查询失败");
        }
        return Result.ok().data("clientInfoList", clientInfoSheetPageInfo);
    }

    @PostMapping("addClient")
    @ApiOperation(value = "客户建档添加操作", notes = "客户建档添加操作", httpMethod = "POST")
    public Result addClient(@RequestBody ClientInfoSheet clientInfoSheet){
        /**
         * @description:客户建档添加操作
         * @param clientInfoSheet
         * @return: com.woniu.bank.util.Result
         * @author: chench
         * @time: 2023/1/4 17:02
         */
        int i = clientInfoService.insertSelective(clientInfoSheet);
        if (i>0) {
            return Result.ok("添加成功");
        }
        return Result.error("添加失败,该用户已经存在");
    }

    @GetMapping("{id}")
    @ApiOperation(value = "客户建档根据id查询操作", notes = "客户建档根据id查询", httpMethod = "GET")
    public Result selectClientById(@PathVariable("id") Long id){
        /**
         * @description:客户建档根据id查询操作
         * @param id
         * @return: com.woniu.bank.util.Result
         * @author: chench
         * @time: 2023/1/4 17:14
         */
        ClientInfoSheet clientInfoSheet = clientInfoService.selectByPrimaryKey(id);
        if (clientInfoSheet==null) {
            return Result.error("查询失败");
        }
        return Result.ok().data("clientInfoSheet",clientInfoSheet);
    }
    @PutMapping("updateClient")
    @ApiOperation(value = "客户建档修改操作", notes = "客户建档根据id修改客户的数据", httpMethod = "PUT")
    public Result updateClient(@RequestBody ClientInfoSheet clientInfoSheet){
        /**
         * @description:客户建档修改操作
         * @param clientInfoSheet
         * @return: com.woniu.bank.util.Result
         * @author: chench
         * @time: 2023/1/4 17:17
         */
        int i = clientInfoService.updateByPrimaryKeySelective(clientInfoSheet);
        if (i>0) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
    @DeleteMapping("{id}")
    @ApiOperation(value = "客户建档删除操作", notes = "客户建档根据id删除客户的数据", httpMethod = "DELETE")
    public Result deleteClient(@PathVariable("id") Long id){
        /**
         * @description:客户建档删除操作
         * @param id
         * @return: com.woniu.bank.util.Result
         * @author: chench
         * @time: 2023/1/4 17:30
         */
        int i = clientInfoService.deleteByPrimaryKey(id);
        if (i>0) {
            return Result.ok("删除成功");
        }
        return Result.error("您输入的id不存在，删除失败");
    }

    @PostMapping("updateStatus")
    @ApiOperation(value = "客户建档批量修改状态", notes = "客户建档批量修改状态", httpMethod = "POST")
    /**
     * @description:客户建档批量修改状态
     * @param status
     * @param clientIds
     * @return: com.woniu.bank.util.Result
     * @author: chench
     * @time: 2023/1/5 16:58
     */
    public Result updateStatus(String status, String clientIds) {
        HashMap<String, Object> map = new HashMap<>();
        List<String> list = Arrays.asList(clientIds.split(","));
        map.put("clientResStatus", status);
        map.put("clientId", list);
        clientInfoService.updateStatus(map);
        return Result.ok();
    }

    @PostMapping("selectKeHu")
    @ApiOperation(value = "评审通过客户查询全部", notes = "评审通过客户查询全部", httpMethod = "POST")
    public Result selectAll(@RequestBody NewClientfoDTO newClientfoDTO) {
        List<ClientInfoSheetRatedVO> clientInfoSheetRatedVOS = clientInfoService.seleteKeHu(newClientfoDTO);
        PageInfo<ClientInfoSheetRatedVO> clientInfoSheetPageInfo = new PageInfo<>(clientInfoSheetRatedVOS );
        if (clientInfoSheetPageInfo==null) {
            return Result.error("查询失败");
        }
        return Result.ok().data("clientInfoList", clientInfoSheetPageInfo);
    }

    @PostMapping("selectGroup")
    @ApiOperation(value = "联保个体用户分组", notes = "联保个体用户分组", httpMethod = "POST")
    public Result selectGroup(@RequestBody NewClientfoDTO newClientfoDTO) {
        Date date = new Date();
        String groupNo = "NDXM"+ date.getTime();
        newClientfoDTO.setGroupNo(groupNo);
        int i = memberService.updateGroup(newClientfoDTO);
        List<ClientInfoSheetRatedVO> clientInfoSheetRatedVOS = clientInfoService.seleteKeHu(newClientfoDTO);
        return Result.ok().data("List",clientInfoSheetRatedVOS);
    }
}
